#!/usr/bin/env python3

import argparse
import os

# Parse args
def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('-tl', '--timeLimit', type=float, default=0.5)
    parser.add_argument('-ml', '--memoryLimit', type=int, default=8)
    parser.add_argument('-b', '--benchmark', type=str)
    parser.add_argument('-m', '--model', type=str, default="")
    parser.add_argument('-e', '--exe', type=str, default="../build/run")
    parser.add_argument('-t', '--type', type=str, choices=["string", "matrix"])
    parser.add_argument('-l', '--log', type=str, default="")
    parser.add_argument('-o', '--output', type=str, default="")
    return parser.parse_args()

if __name__ == "__main__":
    args = parse_args()

    time_limit = args.timeLimit
    memory_limit = args.memoryLimit * 1024 * 1024
    benchmark_file = args.benchmark
    model_file = args.model
    domain = args.type
    bin_file = args.exe
    log_file = args.log
    output_file = args.output

    if len(model_file) == 0: model_file = "../src/train/model/model_" + domain + ".json"
    command = ['ulimit -v ' + str(memory_limit) + ';' + "timeout " + str(time_limit) + " " + bin_file,
               "--model=" + model_file, "--spec=" + benchmark_file, "--type=" + domain]
    if len(log_file) > 0: command.append("--log=" + log_file)
    if len(output_file) > 0: command.append("--oup=" + output_file)
    os.system(" ".join(command))